package com.tingyuge.thinking.spring.resource;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;

import java.io.File;
import java.io.IOException;
import java.io.Reader;

/**
 * 带有字符编码的 {@link FileSystemResourceLoader} 示例
 * @author xiaodao
 * @version 1.0
 * @date 2021/8/22
 * @see EncodedResource
 */
public class EncodedFileSystemResourceLoaderDemo {

    public static void main(String[] args) throws IOException {
        String currentJavaPath = System.getProperty("user.dir") + "" + "/resource/src/main/java/com/tingyuge/thinking/spring/resource/EncodedFileSystemResourceDemo.java";
        FileSystemResourceLoader fileSystemResource = new FileSystemResourceLoader();
        Resource resource = fileSystemResource.getResource(currentJavaPath);
        EncodedResource encodedResource = new EncodedResource(resource, "utf-8");

        //字符输入流
      try(Reader reader = encodedResource.getReader()) {
          System.out.println(IOUtils.toString(reader));
      }


    }
}
